package com.design.pattern.singleton.type2;


public class SingletonTest2 {

    public static void main(String[] args) {
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton1);
        System.out.println(singleton2);
        System.out.println(singleton1 == singleton2);
    }
}

//饿汉式（静态代码块）
class Singleton {
    //1.构造函数私有化
    private Singleton() {
    }

    //2、本类内部创建对象实例
    private static final Singleton instance;


    static {// 在静态代码块中，创建单例对象
        instance = new Singleton();
    }

    //3、提供一个公有的静态方法，返回实例对象
    public static Singleton getInstance() {
        return instance;
    }

}
